package com.haofengsoft.scce.adapter.information;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;

/**
 * 为ViewPager添加布局（Fragment），绑定和处理fragments和viewpager之间的逻辑关系
 */
public class InformationAttentionAdapter extends FragmentStatePagerAdapter {

    private FragmentManager fragmentManager;
    private List<InformationAttentionModel> informationAttentionModelList;
    private int currentPageIndex = 0; // 当前page索引（切换之前）

    public InformationAttentionAdapter(FragmentManager fragmentManager,
                                       List<InformationAttentionModel> informationAttentionModelList) {
        super(fragmentManager);
        this.informationAttentionModelList = informationAttentionModelList;
        this.fragmentManager = fragmentManager;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        Object obj = super.instantiateItem(container, position);
        return obj;
    }

    public void setFragments(List<InformationAttentionModel> modelList) {
        if (this.informationAttentionModelList != null) {
            FragmentTransaction ft = fragmentManager.beginTransaction();
            for (int i = 0; i < informationAttentionModelList.size(); i++) {
                ft.remove(informationAttentionModelList.get(i).getFragment());
            }
            ft.commit();
            ft = null;
            fragmentManager.executePendingTransactions();
        }
        this.informationAttentionModelList = modelList;
        notifyDataSetChanged();
    }

    public String getPageTitle(int position) {
        return informationAttentionModelList.get(position).getTitle();
    }

    @Override
    public int getCount() {
        return informationAttentionModelList.size();
    }

    @Override
    public Fragment getItem(int position) {
        return informationAttentionModelList.get(position).getFragment();
    }


    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }


}
